package com.demo.woziqi;

import com.demo.woziqi.server.GameThread;
import com.demo.woziqi.util.GameConstant;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-05-23
 * <p>
 * 联机模式，信息设置类
 */
public class GameInputPortAndIPStage extends Stage {
    private Button okBtn;// 确定
    private Button noBtn;// 取消
    private Label DIp; // 对方IP
    private Label DDk; // 对方端口
    private Label WIp;// 我方IP
    private Label WDk; // 我方端口
    private TextField DIpField;// 对方IP
    private TextField DDkField;// 对方端口
    private TextField WIpField;// 我方IP
    private TextField WDkField;// 我方端口

    public static Stage inputStage;

    public GameInputPortAndIPStage() {
        inputStage = this;
        setTitle("IP/端口");
        Pane panes = new Pane();
        setHeight(400);
        setWidth(500);
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost(); // 获取本机IP
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        this.DIp = new Label("对 方 IP：");
        this.DDk = new Label("对方端口：");
        this.WIp = new Label("我 方 IP：");
        this.WDk = new Label("我方端口：");
        this.DIpField = new TextField("127.0.0.1");// 设置本机IP
        this.DDkField = new TextField("8888");
        this.WIpField = new TextField(addr.getHostAddress());// 设置本机IP
//        this.WIpField = new TextField("127.0.0.1");// 设置本机IP
        this.WDkField = new TextField("9999");
        this.okBtn = new Button("确   定");
        this.noBtn = new Button("取   消");

        Font font = Font.font("KaiTi", FontWeight.BOLD, FontPosture.ITALIC, 25);
        DIp.setLayoutX(40);
        DIp.setLayoutY(50);
        DIp.setFont(font);
        DIpField.setLayoutX(170);
        DIpField.setLayoutY(50);
        DIpField.setPromptText("请输入对方IP");

        DDk.setLayoutX(40);
        DDk.setLayoutY(100);
        DDk.setFont(font);
        DDkField.setLayoutX(170);
        DDkField.setLayoutY(100);
        DDkField.setPromptText("请输入对方端口");

        WIp.setLayoutX(40);
        WIp.setLayoutY(150);
        WIp.setFont(font);
        WIpField.setLayoutX(170);
        WIpField.setLayoutY(150);
        WIpField.setPromptText("请输入本机IP");

        WDk.setLayoutX(40);
        WDk.setLayoutY(200);
        WDk.setFont(font);
        WDkField.setLayoutX(170);
        WDkField.setLayoutY(200);
        WDkField.setPromptText("请设置本机端口");

        okBtn.setLayoutX(110);
        okBtn.setLayoutY(270);
        noBtn.setLayoutX(300);
        noBtn.setLayoutY(270);

        panes.getChildren().add(DIp);
        panes.getChildren().add(DDk);
        panes.getChildren().add(WIp);
        panes.getChildren().add(WDk);
        panes.getChildren().add(DIpField);
        panes.getChildren().add(DDkField);
        panes.getChildren().add(WIpField);
        panes.getChildren().add(WDkField);
        panes.getChildren().add(okBtn);
        panes.getChildren().add(noBtn);

        Scene scene = new Scene(panes); // 场景，存放面板
        scene.getStylesheets().add(String.valueOf(getClass().getClassLoader().getResource("scratch.css"))); //引入css
        this.setScene(scene); // 将场景设置到主舞台

        okBtn.setOnMouseClicked(e -> {
            GameConstant.DIpText = DIpField.getText();
            GameConstant.DDkText = Integer.parseInt(DDkField.getText());
            GameConstant.WIpText = WIpField.getText();
            GameConstant.WDkText = Integer.parseInt(WDkField.getText());

            this.close(); // 关闭当前窗口
            // 启动服务器端线程
            GameFightStage gameFightStage = new GameFightStage();
            new Thread(new GameThread(gameFightStage)).start(); // 启动服务器线程
            System.out.println("启动端口：" + GameConstant.WDkText);
        });
        noBtn.setOnMouseClicked(e -> {
            this.close(); // 关闭当前窗口
            GoApplicationStage.stage.show(); // 显示上一个窗口，程序模式选择窗口
        });
        this.setOnCloseRequest(e -> { // 关闭窗口事件监听
            GoApplicationStage.stage.show(); // 显示程序模式选择窗口
            this.close();
        });
    }

}
